package com.ybxx.fees.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collection;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.StatementCallback;

import com.ybxx.core.command.QueryFilter;
import com.ybxx.core.dao.impl.BaseDaoImpl;
import com.ybxx.core.util.UUIDGenerator;
import com.ybxx.core.web.paging.PagingBean;
import com.ybxx.fees.dao.WatermeterRecordsDao;
import com.ybxx.fees.model.AnmeterRecordItem;
import com.ybxx.fees.model.AnmeterRecords;
import com.ybxx.fees.model.WatermeterRecordItem;
import com.ybxx.fees.model.WatermeterRecords;

public class WatermeterRecordsDaoImpl extends BaseDaoImpl<WatermeterRecords>
		implements WatermeterRecordsDao {

	public WatermeterRecordsDaoImpl() {
		super(WatermeterRecords.class);
		// TODO Auto-generated constructor stub
	}
	public WatermeterRecords generator(final String sql) {
		return super.jdbcTemplate.execute(new StatementCallback<WatermeterRecords>(){
			public WatermeterRecords doInStatement(Statement stat)
					throws SQLException, DataAccessException {
				ResultSet rs=stat.executeQuery(sql);
				WatermeterRecords watermeterRecords=null;
				if(!rs.next()){
					return null;
				}
				watermeterRecords=new WatermeterRecords();
				watermeterRecords.setWmfcode(UUIDGenerator.getUUID());
				watermeterRecords.setThereadings(rs.getString("thereadings"));
				watermeterRecords.setCurrentreading(rs.getString("currentreading"));
				watermeterRecords.setWalkquantity(rs.getString("walkquantity"));
				watermeterRecords.setAmount(rs.getString("amount"));
				watermeterRecords.setCharge(rs.getString("actual"));
				float count=0.0f;
				do{
					WatermeterRecordItem item=new WatermeterRecordItem();
					item.setRicode(UUIDGenerator.getUUID());
					item.setWatermeterRecords(watermeterRecords);
					item.setTarget(rs.getString("target"));
					item.setPrice(rs.getString("price"));
					item.setWalk(rs.getString("meter"));
					item.setYmoney(rs.getString("fees"));
					count=count+Float.parseFloat((item.getYmoney()==null || "".equals(item.getYmoney()))?"0":item.getYmoney());
					watermeterRecords.getItems().add(item);
				}while(rs.next());
				watermeterRecords.setReceivable(count+"");
				return watermeterRecords;
			}
			
		});
	}
}
